<template>
  <div class='container' ref='detailBox' @scroll="remPos">
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{detail.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="detail.aut_photo" />
        <div class="text">
          <p class="name">{{detail.aut_name}}</p>
          <p class="time">{{detail.pubdate|relTime}}</p>
        </div>
        <van-button round size="small" type="info" @click="followAut">{{detail.is_followed ? '已关注' : '+ 关注'}}</van-button>
      </div>
      <!-- 内容渲染区域 -->
      <div class="content" v-html="detail.content"></div>
      <div class="zan">
        <van-button round size="small" :class="{active:detail.attitude===1}" plain icon="like-o" @click="changeAtt(1)">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" :class="{active:detail.attitude===0}" plain icon="delete" @click="changeAtt(0)">不喜欢</van-button>
      </div>
      <!-- 文章评论列表 -->
      <Comment />
    </div>
  </div>
</template>

<script>
import { getDetailById, disLike, unDisLike, like, unLike } from '@/api/article'
import { follow, unFollow } from '@/api/user'
// 导入评论组件
import Comment from './components/comment'
// 缓存的组件在什么时候获取数据？
export default {
  name: 'articleDetail',
  components: {
    Comment
  },
  data () {
    return {
      // 文章详情数据
      detail: {},
      // 阅读位置
      scrollTop: 0
    }
  },
  methods: {
    // 获取文章详情
    async getDetail () {
      try {
        const { data } = await getDetailById(this.$route.params.id)
        // console.log(data)
        this.detail = data
        // console.log(data.aut_photo)
      } catch (error) {

      }
    },
    // 记录阅读位置
    remPos (e) {
      this.scrollTop = e.target.scrollTop
    },
    // 关注/取关
    async followAut () {
      // eslint-disable-next-line camelcase
      const { is_followed, aut_id } = this.detail
      try {
        // eslint-disable-next-line camelcase
        if (is_followed) {
          // 取关
          await unFollow(aut_id)
          this.$toast.fail('取消关注')
          // 前端页面
          this.detail.is_followed = false
        } else {
          // 关注
          await follow(aut_id)
          this.$toast.success('关注成功')
          // 前端页面
          this.detail.is_followed = true
        }
      } catch (error) {
        this.$toast.fail(error.message)
      }
    },
    // 针对文章处理点赞/不喜欢
    async changeAtt (type) {
      // eslint-disable-next-line camelcase
      const { attitude, art_id } = this.detail
      try {
        if (type) {
          // type=1 处理点赞
          if (attitude === 1) {
            // 取消点赞
            await like(art_id)
            // 前端页面变为中立状态
            this.detail.attitude = -1
          } else {
            // 点赞
            await unLike(art_id)
            // 前端页面变为点赞状态
            this.detail.attitude = 1
          }
        } else {
          // type=0 处理不喜欢
          if (attitude === 0) {
            // 取消不喜欢
            await disLike(art_id)
            // 前端页面变为中立状态
            this.detail.attitude = -1
          } else {
            // 确认不喜欢
            await unDisLike(art_id)
            // 前端页面变为点赞状态
            this.detail.attitude = 0
          }
        }
        this.$toast.success('操作成功')
      } catch (error) {
        console.log(error)
        this.$toast.fail('操作失败')
      }
    }
  },
  created () {
    // 组件被缓存了，这里只会执行一次
    // 如果在这调用详情接口，页面被缓存了，不会触发这个狗子
    // console.log('123')
  },
  //  执行时机：1.默认执行一次，2、每次进入组件都会执行一次
  activated () {
    if (this.detail.art_id?.toString() !== this.$route.params.id) {
      // 如果上次文章id和当次访问id不一样，则需要重新获取文章详情数据
      // 重置之前滚动记录
      this.scrollTop = 0
      this.getDetail()
    } else {
      // 一样则执行阅读记忆
      /**
       * 1、阅读记忆
       * 2、下次再读这篇文章时
       */
      this.$refs.detailBox.scrollTop = this.scrollTop
    }
  },
  // 缓存后=》离开页面执行
  deactivated () {

  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  height: 1000%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan {
    text-align: center;
    padding: 10px 0;
    .active {
      border-color: red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      text-align: left;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  // 内容中包含：img 特别宽  code pre 不能换行
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
    /deep/ code {
      white-space: pre-wrap;
    }
    /deep/ pre {
      white-space: pre-wrap;
    }
  }
}
</style>
